﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace System
{
    public static class NumberExtend
    {
        public static int toDefault(this int? value)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                return value.Value;
            }
        }

        public static String toDefaultRate(this int? value)
        {
            if (value == null)
            {
                return "0%";
            }
            else
            {
                return value.Value+"%";
            }
        }

        public static String toRate(this int? value)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                return value.Value + "%";
            }
        }


        public static double toDefault(this double? value)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                return value.Value;
            }
        }

        public static String toDefaultRate(this double? value)
        {
            if (value == null)
            {
                return "0%";
            }
            else
            {
                return value.Value + "%";
            }
        }

        public static String toRate(this double? value)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                return value.Value + "%";
            }
        }


        public static decimal toDefault(this decimal? value)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                return value.Value;
            }
        }

        public static String toDefaultRate(this decimal? value)
        {
            if (value == null)
            {
                return "0%";
            }
            else
            {
                return value.Value + "%";
            }
        }

        public static String toRate(this decimal? value)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                return value.Value + "%";
            }
        }

        public static String toInt(this decimal? value, int jd = 1)
        {
            if (value == null)
            {
                return "";
            }
            decimal d=Decimal.Round(value.Value,jd);
            if (d == (int)d)
            {
                return ((int)d).ToString();
            }
            return d.ToString().TrimEnd('0');
        }

        public static String toInt(this decimal value,int jd=1)
        {
            decimal d = Decimal.Round(value, jd);
            if (d == (int)d)
            {
                return ((int)d).ToString();
            }
            return Decimal.Round(d, jd).ToString().TrimEnd('0');
        }


        public static String toUpInt(this decimal? value)
        {
            if (value == null)
            {
                return "";
            }
            decimal d = Decimal.Ceiling(value.Value);
            return d.ToString();
        }

        public static String toUpInt(this decimal value)
        {
            decimal d = Decimal.Ceiling(value);
            return d.ToString();
        }
        public static int AutoCeling(this decimal value)
        {
            if (value >= 0) return (int)decimal.Ceiling(value);
            return (int)decimal.Floor(value);
        }
        public static int AutoRound(this decimal value)
        {
            return (int)decimal.Round(value);
            //if (value >= 0) return (int)decimal.Ceiling(value);
            //return (int)decimal.Floor(value);
        }
        public static int AutoRound1(this decimal value)
        {
            if (value >= 0)
                return (int)Math.Round(value, 0, MidpointRounding.AwayFromZero);
            return (int)Math.Round(value, 0, MidpointRounding.ToEven);
        }

        public static string toRound(this decimal value,int jd=1)
        {
            if (value >= 0)
                return Math.Round(value, jd, MidpointRounding.AwayFromZero).ToString();
            return Math.Round(value, jd, MidpointRounding.ToEven).ToString();
        }

    }
}
